package com.zy.swagger.util;

import com.zy.swagger.entity.sys.SysUser;
import com.zy.swagger.exception.BaseException;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * description 当前登录用户
 *
 * @author ZY
 * @date 2021年04月01日 00:25
 **/

public class LoginUserUtil {

    public static SysUser getCurrentUser() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal == null) {
            throw new BaseException(401, "未登录，无权限操作");
        }
        return (SysUser) principal;
    }

    public static Integer getCurrentUserId() {
        return getCurrentUser().getId();
    }

}
